package com.nand.library.base.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * created by 86185
 * on 2020/2/26
 */
public class SpUtils {
    private static SharedPreferences sp;
    private final static String APP_NAME_TAG = "YCCALLISFIRST";


    /**
     * 用于保存集合
     *
     * @param key  key
     * @param list 集合数据
     * @return 保存结果
     */
    public static <T> boolean putListData(String key, List<T> list) {
        boolean result = false;
        SharedPreferences.Editor editor = sp.edit();
        if (editor != null) {
            if (0 == list.size()){
                return false;
            }
            String type = list.get(0).getClass().getSimpleName();
            JsonArray array = new JsonArray();
            try {
                switch (type) {
                    case "Boolean":
                        for (int i = 0; i < list.size(); i++) {
                            array.add((Boolean) list.get(i));
                        }
                        break;
                    case "Long":
                        for (int i = 0; i < list.size(); i++) {
                            array.add((Long) list.get(i));
                        }
                        break;
                    case "Float":
                        for (int i = 0; i < list.size(); i++) {
                            array.add((Float) list.get(i));
                        }
                        break;
                    case "String":
                        for (int i = 0; i < list.size(); i++) {
                            array.add((String) list.get(i));
                        }
                        break;
                    case "Integer":
                        for (int i = 0; i < list.size(); i++) {
                            array.add((Integer) list.get(i));
                        }
                        break;
                    default:
                        Gson gson = new Gson();
                        for (int i = 0; i < list.size(); i++) {
                            JsonElement obj = gson.toJsonTree(list.get(i));
                            array.add(obj);
                        }
                        break;
                }
                editor.putString(key, array.toString());
                result = true;
            } catch (Exception e) {
                result = false;
                e.printStackTrace();
            }
            editor.apply();
        }
        return result;
    }
    /**
     * 获取保存的List
     *
     * @param key key
     * @return 对应的Lis集合
     */
    public static <T> List<T> getListData(String key, Class<T> cls) {
        List<T> list = new ArrayList<>();
        String json = sp.getString(key, "");
        if (!json.equals("") && json.length() > 0) {
            Gson gson = new Gson();
            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            for (JsonElement elem : array) {
                list.add(gson.fromJson(elem, cls));
            }
        }
        return list;
    }

    /**
     * 写字符串
     *
     * @param context
     * @param key
     * @param value
     */
    public static void writeString(Context context, String key, String value) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        sp.edit().putString(key, value).apply();

    }

    /**
     * 写布尔值
     *
     * @param context
     * @param key
     * @param value
     */
    public static void writeBoolean(Context context, String key, boolean value) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    /**
     * 写int值
     *
     * @param context
     * @param key
     * @param value
     */
    public static void writeInt(Context context, String key, int value) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        sp.edit().putInt(key, value).commit();
    }

    /**
     * 读取字符串
     *
     * @param context
     * @param key
     * @param defaultValue
     * @return
     */
    public static String readString(Context context, String key, String defaultValue) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        return sp.getString(key, defaultValue);
    }

    /**
     * 读取boolean
     *
     * @param context
     * @param key
     * @param defaultValue
     * @return
     */
    public static boolean readBoolean(Context context, String key, boolean defaultValue) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defaultValue);
    }

    /**
     * 读取int
     *
     * @param context
     * @param key
     * @param defaultValue
     * @return
     */
    public static int readInt(Context context, String key, int defaultValue) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        return sp.getInt(key, defaultValue);
    }



    public static void clearOne(Context context ,String key) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN",  Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.apply();
    }
    /**
     * 获取登录电话号码
     *
     * @param context
     * @return
     */
    public static String getUserPhone(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        return sp.getString("login_phone", null);
    }

    public static String getUserId(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        return sp.getString("userId", null);
    }

    /**
     * 获取token
     *
     * @param context
     * @return
     */
    public static String getToken(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        return sp.getString("token", null);
    }

    /**
     * 获取个推cid
     *
     * @param context
     * @return
     */
    public static String getGeTuiCid(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        return sp.getString("getui_cid", null);
    }

    public static void clearInfo(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        sp.edit().clear().apply();
    }


    /**
     * 存储是不是第一次
     *
     * @param context
     * @param key
     * @param value
     */
    public static void writeIsFist(Context context, String key, boolean value) {
        context.getSharedPreferences(APP_NAME_TAG, Context.MODE_PRIVATE).edit().putBoolean(key, value).apply();
    }

    /**
     * 获取是不是第一次
     *
     * @param context
     * @param key
     * @param defaultValue
     */
    public static boolean readIsFirst(Context context, String key, boolean defaultValue) {
        return context.getSharedPreferences(APP_NAME_TAG, Context.MODE_PRIVATE).getBoolean(key, defaultValue);
    }

    /**
     * 获取用户头像
     *
     * @param context
     * @return
     */
    public static String getUserHeaderImg(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        String userHeader = sp.getString("userHeader", null);
        if (TextUtils.isEmpty(userHeader)) {
            return null;
        }
        return userHeader;
    }

    /**
     * 获取用户昵称
     *
     * @param context
     * @return
     */
    public static String getUserNickName(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        String nick_name = sp.getString("nick_name", null);
        if (TextUtils.isEmpty(nick_name)) {
            return null;
        }
        return nick_name;
    }

    /**
     * 存储不需要再提示更新的版本号
     *
     * @param version
     */
    public static void saveVersion(Context context, int version) {
        context.getSharedPreferences("Version", Context.MODE_PRIVATE).edit().putInt("version", version).apply();

    }


    /**
     * 获取不需要提示更新的版本号
     *
     * @param context
     * @return
     */
    public static int getSaveVersion(Context context) {
        return context.getSharedPreferences("Version", Context.MODE_PRIVATE).getInt("version", 0);
    }

    /**
     * 是否实名认证
     *
     * @param context
     * @return 返回值共有3中状态, 分别是0(既没有实名认证也没有密码)
     * 1(实名了并且有密码)
     * 2(实名了但是没有密码)
     */
    public static int isReal(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        return sp.getInt("isReal", 0);
    }

    /**
     * 获取真实姓名
     *
     * @param context
     * @return
     */
    public static String getRealName(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("HONGXIN", Context.MODE_PRIVATE);
        }
        return sp.getString("realName", null);
    }
}

